

$(function()
{
	$('input#page-go').hide();
	
	$('select#page-topic').change(function(){
		var url = "annonces-annonces-page" + $(this).val() + ".html";
		document.location.href = url;
	});
	
	$('select#page-post').change(function(){
		var url = "annonces-voir-" + $("#post-id").val() + "-page" + $(this).val() + ".html";
		document.location.href = url;
	});
	
	// masquer la demande de confirmation
	$('a.js-hide').click(function() {
		$('span.post-delete').fadeOut();
	});
	
	// demander confirmation
	$('a.js-ask-validation').click(function() {
		$('span.post-delete').hide();
		$(this).parent().children('span.post-delete').css('display', 'block');
	});

	$('#submit-preview').click(function() {
		$('#preview-ajax').html('<img src="images/ajax-loader.gif" alt="..." />');
		var content = $('#forum-zone-edit').val();
		$.ajax({
			url: 'pages/annonces/_forum_ajax.php',
			type: 'POST',
			data: 'action=preview'
			    + '&content='+content,
				
			success: function(result) {
				$('#preview-ajax').html(result);
			}
		});
		
		return false;
	});
	
	// supprimer une réponse
	$('a.js-del-answer').click(function() {
		var id = $(this).attr('id').split('_')[1];
		var self = this;
		$.ajax({
			url: 'pages/annonces/_forum_ajax.php',
			type: 'POST',
			data: 'action=delete_answer'
				+ '&id=' + id,
			
			success: function(result) {
				window.location.reload();
			}
		});
	});
	
});


/**
 * Insérer un tag au niveau du curseur
 * @param[in] text1: tag ouvrant
 * @param[in] text2: tag fermant
 */
function insert_tag(text1, text2)
{
	var tag = document.getElementById("forum-zone-edit");
	if (document.selection)
	{ // IE
		var str = document.selection.createRange().text;
		tag.focus();
		var sel = document.selection.createRange();
		if (text2 != "")
		{
			if (str == "")
			{
				sel.text = sel.text + text1 + text2;
				sel.moveStart("character", -text2.length);
				sel.moveEnd("character", -text2.length);
				sel.select();
			}
			else
			{
				sel.text = text1 + sel.text + text2;
			}
		}
		else
		{
			sel.text = sel.text + text1;
		}
	}
	else if (tag.selectionStart || tag.selectionStart == 0)
	{ // Gecko
		if (tag.selectionEnd > tag.value.length)
		{
			tag.selectionEnd = tag.value.length;
		}
		var firstPos = tag.selectionStart;
		var secondPos = tag.selectionEnd + text1.length;
		var contenuScrollTop = tag.scrollTop;

		tag.value = tag.value.slice(0, firstPos)  + text1 + tag.value.slice(firstPos);
		tag.value = tag.value.slice(0, secondPos) + text2 + tag.value.slice(secondPos);

		tag.selectionStart = firstPos + text1.length;
		tag.selectionEnd = secondPos;
		tag.focus();
		tag.scrollTop = contenuScrollTop;
	}
	else
	{ // Opera
		var sel = document.hop.contenu;
		var inst = countInstances(text1, text2);
		if (inst % 2 != 0 && text2 != "")
		{
			sel.value = sel.value + text2;
		}
		else
		{
			sel.value = sel.value + text1;
		}
	}
}


function hide_write_tools()
{
	$('#forum-write-tools-on').slideUp();
	$('#forum-write-tools-off').fadeIn();
}

function show_write_tools()
{
	$('#forum-write-tools-on').slideDown();
	$('#forum-write-tools-off').fadeOut();	
}




